package com.liuhengjia.mapper;

import com.liuhengjia.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserMapper {
    /**
     * 用户名查询用户
     *
     * @param username 用户名
     * @return com.liuhengjia.pojo.User
     */
    @Select("SELECT id, username, password, nickname, email, user_pic, create_time, update_time " +
            "FROM user " +
            "WHERE username = #{username}")
    User selectByUsername(String username);

    /**
     * 插入一个元组
     *
     * @param username 用户名
     * @param password 密码
     */
    @Insert("INSERT INTO user (username, password, create_time, update_time) " +
            "VALUES (#{username}, #{password}, now(), now())")
    void insertOne(String username, String password);

    /**
     * 更新用户基本信息
     *
     * @param user 用户实体对象
     */
    @Update("UPDATE user " +
            "SET nickname = #{nickname}, email = #{email}, update_time = #{updateTime} " +
            "WHERE id = #{id}")
    void update(User user);

    /**
     * 更新用户头像
     *
     * @param avatarUrl 头像路径
     */
    @Update("UPDATE user " +
            "SET user_pic = #{avatarUrl}, update_time = now() " +
            "WHERE id = #{id}")
    void updateAvatar(String avatarUrl, Integer id);

    /**
     * 更新用户密码
     *
     * @param md5NewPwd MD5新密码字符串
     * @param id        用户 ID
     */
    @Update("UPDATE user " +
            "SET password = #{md5NewPwd}, update_time = now() " +
            "WHERE id = #{id}")
    void updatePwd(String md5NewPwd, Integer id);
}
